package Arrays;

import java.util.HashSet;
import java.util.Set;

public class _349_IntersectionofTwoArrays {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> n1 = new HashSet<Integer>();
        Set<Integer> intersection = new HashSet<Integer>();
        for(int i=0;i<nums1.length;i++){
            n1.add(nums1[i]);
        }
        for(int j=0;j<nums2.length;j++){
            if(n1.contains(nums2[j])){
                intersection.add(nums2[j]);
            }
        }
        int i = 0;
        int [] result = new int[intersection.size()];
        for(Integer val:intersection){
            result[i++] = val;
        }
        return result;
    }
}
